package com.cencat.common.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 统一响应结果类
 * 
 * @author cencat
 * @since 2024
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
    
    /**
     * 响应码
     */
    private Integer code;
    
    /**
     * 响应消息
     */
    private String message;
    
    /**
     * 响应数据
     */
    private T data;
    
    /**
     * 时间戳
     */
    private Long timestamp;
    
    /**
     * 请求ID（用于链路追踪）
     */
    private String requestId;
    
    /**
     * 创建Builder实例
     */
    public static <T> ResultBuilder<T> builder() {
        return new ResultBuilder<T>();
    }
    
    /**
     * Builder类
     */
    public static class ResultBuilder<T> {
        private Integer code;
        private String message;
        private T data;
        private Long timestamp;
        private String requestId;
        
        public ResultBuilder<T> code(Integer code) {
            this.code = code;
            return this;
        }
        
        public ResultBuilder<T> message(String message) {
            this.message = message;
            return this;
        }
        
        public ResultBuilder<T> data(T data) {
            this.data = data;
            return this;
        }
        
        public ResultBuilder<T> timestamp(Long timestamp) {
            this.timestamp = timestamp;
            return this;
        }
        
        public ResultBuilder<T> requestId(String requestId) {
            this.requestId = requestId;
            return this;
        }
        
        public Result<T> build() {
            Result<T> result = new Result<T>();
            result.code = this.code;
            result.message = this.message;
            result.data = this.data;
            result.timestamp = this.timestamp;
            result.requestId = this.requestId;
            return result;
        }
    }
    
    /**
     * 成功响应
     */
    public static <T> Result<T> success(T data) {
        return Result.<T>builder()
                .code(200)
                .message("success")
                .data(data)
                .timestamp(System.currentTimeMillis())
                .build();
    }
    
    /**
     * 成功响应（无数据）
     */
    public static <T> Result<T> success() {
        return success(null);
    }
    
    /**
     * 成功响应（带消息）
     */
    public static <T> Result<T> success(String message) {
        return Result.<T>builder()
                .code(200)
                .message(message)
                .timestamp(System.currentTimeMillis())
                .build();
    }
    
    /**
     * 失败响应
     */
    public static <T> Result<T> error(String message) {
        return Result.<T>builder()
                .code(500)
                .message(message)
                .timestamp(System.currentTimeMillis())
                .build();
    }
    
    /**
     * 失败响应（带错误码）
     */
    public static <T> Result<T> error(Integer code, String message) {
        return Result.<T>builder()
                .code(code)
                .message(message)
                .timestamp(System.currentTimeMillis())
                .build();
    }
    
    /**
     * 判断是否成功
     */
    public boolean isSuccess() {
        return code != null && code == 200;
    }
    
    /**
     * 判断是否失败
     */
    public boolean isError() {
        return !isSuccess();
    }
}